#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
SVN管理系统，增加项目，管理用户
徐晨阳
2007-11-11
'''
import os
import ConfigParser

class svn(object):
    '''
    SVN管理
    '''
    def __init__(self, path='/home/svn', user='svn'):
        self.path=path
        self.user=user

    def createProject(self, name):
        '''
        创建项目
        返回值=0成功
        '''
        return os.system('su %s -c "svnadmin create %s/%s"'%(self.user, self.path, name))

    def setProject(self, name, anon='none', auth='write'):
        '''
        设置项目权限
        返回值=0成功
        '''
        s='%s/%s/conf/svnserve.conf'%(self.path, name)
        if os.path.isfile(s):
            config = ConfigParser.ConfigParser()
            config.readfp(open(s))
            config.set("general", "password-db", "passwd")
            config.set("general", "anon-access", anon)
            config.set("general", "auth-access", auth)
            config.write(open(s, "w"))
            return 0
        else:
            return 1

    def setUser(self, name, users):
        '''
        设置项目用户
        返回值=0成功
        '''
        s='%s/%s/conf/passwd'%(self.path, name)
        if os.path.isfile(s):
            config = ConfigParser.ConfigParser()
            config.readfp(open(s))
            config.remove_section('users')
            config.add_section('users')
            for u in users:
                config.set("users",u,users[u])
            config.write(open(s, "w"))
            return 0
        else:
            return 1

    def delUser(self, name, user):
        '''
        删除项目用户
        '''
        s='%s/%s/conf/passwd'%(self.path, name)
        if os.path.isfile(s):
            config = ConfigParser.ConfigParser()
            config.readfp(open(s))
            config.remove_option('users', user)
            config.write(open(s, 'w'))
            return 0
        else:
            return 1

    def setPassword(self, name, user, password):
        '''
        设置用户密码
        '''
        s='%s/%s/conf/passwd'%(self.path, name)
        if os.path.isfile(s):
            config = ConfigParser.ConfigParser()
            config.readfp(open(s))
            config.set('users', user, password)
            config.write(open(s, 'w'))
            return 0
        else:
            return 1
            
